package mis.ds.ps.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainForm extends JFrame implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final int	ITEM_PLAIN	=	0;	// Item types
	private final int	ITEM_CHECK	=	1;
	private final int	ITEM_RADIO	=	2;
	
	private JDesktopPane desktop;
	
	private JMenuBar menubar;
	private JMenu menuFile;	
	private JMenuItem menuFileCarga;
	private JMenuItem menuFilePreferencias;
	private JMenuItem menuFileExit;
	private JMenu menuHelp;
	private JMenuItem menuHelpAbout;
	
	private ImportForm importCarga;
	
	public MainForm() {
		super("TimeTable Application");
//		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        setBounds(0,0,screenSize.width, screenSize.height);
		
		//Make the big window be indented 50 pixels from each edge
        //of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width  - inset*2,
                  screenSize.height - inset*2);
        
      //Set up the GUI.
        desktop = new JDesktopPane(); //a specialized layered pane
        setContentPane(desktop);

        //Make dragging a little faster but perhaps uglier.
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        
        addMenu();

	}
	
	private void addMenu() {
		// Create the menu bar
		menubar = new JMenuBar();
		
		// Set this instance as the application's menu bar
		setJMenuBar(menubar);
		
		// Build the File sub-menu
		menuFile = new JMenu("File");
		menuFile.setMnemonic('F');
		menubar.add(menuFile);
		
		// Create File items
//		menuFileCarga = new JMenuItem("Carga");
//		menuFileCarga.addActionListener(this);
//		menuFile.add(menuFileCarga);
		
		menuFileCarga = CreateMenuItem(menuFile, ITEM_PLAIN, 
				"Carga...", null, 'C', null);
		
		menuFilePreferencias = CreateMenuItem(menuFile, ITEM_PLAIN, 
				"Preferências...", null, 'P', null);
		
		menuFile.addSeparator();
		
//		menuFileExit = new JMenuItem("Exit");
//		menuFile.add(menuFileExit);				
//		menuFileExit.addActionListener(this);
		
		menuFileExit = CreateMenuItem( menuFile, ITEM_PLAIN,
				"Exit", null, 'x',
				"Exit Application" );
		
		// Build the File sub-menu
		menuHelp = new JMenu("Help");
		menuHelp.setMnemonic('H');
		menubar.add(menuHelp);
		
		menuHelpAbout = CreateMenuItem(menuHelp, ITEM_PLAIN, 
				"About...", null, 'A', "Aplication credits....");
				
	}
	
	public JMenuItem CreateMenuItem(JMenu menu, int iType, String sText,
			ImageIcon image, int acceleratorKey, String sToolTip) {
		
		// Create the item
		JMenuItem menuItem;
		
		switch(iType) {
			case ITEM_RADIO:
				menuItem = new JRadioButtonMenuItem();
				break;
			
			case ITEM_CHECK:
				menuItem = new JCheckBoxMenuItem();
				break;
			
			default:
				menuItem = new JMenuItem();
				break;
		}
		
		// Add the item test
		menuItem.setText(sText);
		
		// Add the optional icon
		if(image != null)
		menuItem.setIcon(image);
		
		// Add the accelerator key
		if(acceleratorKey > 0)
		menuItem.setMnemonic(acceleratorKey);
		
		// Add the optional tool tip text
		if(sToolTip != null)
		menuItem.setToolTipText(sToolTip);
		
		// Add an action handler to this menu item
		menuItem.addActionListener(this);
		
		menu.add(menuItem);
		
		return menuItem;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getActionCommand());
		if (e.getActionCommand() == "Carga...") {
			importCarga = new ImportForm();
			importCarga.setVisible(true);
	        desktop.add(importCarga);
		} else if (e.getActionCommand() == "Exit") {
			System.exit(0);
		}
		
	}

	private static void createMainGUI() {
		MainForm that = new MainForm();
		that.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		that.setVisible(true);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
//            	UIManager.put("swing.boldMetal", Boolean.FALSE);
            	createMainGUI();
            }
        });
	}
}
